GBML manual / Data Containers

Data containers

One family of seven bounded containers - array, list, grid, stack, queue, map and set - and how to pick the right one.

What a data container is

Most of the time a variable holds one value - an int, a string, a ref<obj>. A data container holds many values at once, under one name. GBML has seven of them: array<T>, list<T>, grid<T>, stack<T>, queue<T>, map<K,V>, and set<T> for "which of these have I already seen." They look like seven different things with seven different rulebooks, but they are really one family with one shared rulebook (below) and a handful of honest differences that decide which one to reach for.

Every container on this page is covered in full on its own pages - this one is the map of the family, not a replacement for them.

Picking one

ContainerShapeReach it byExtra hardware ruleReach for it when
array<T>a fixed set of slotsa[i], or array_length/array_copy/array_sort/array_shuffle/array_foreach/array_filternoneyou already know how many, and that count never changes - a fixed inventory of five weapon slots
list<T>items that come and go, in ordera[i], or list_add/list_insert/list_remove/list_fill/list_create_fill/list_copy/list_contains/list_sort/list_shuffle/list_foreach/list_filter/list_keepnonean inventory, a high-score table, "the last few things that happened"
grid<T>a fixed 2-D block of cellsboard[# x, y], or grid_width/grid_height/grid_fill/grid_fill_region/grid_get_region_sum/grid_get_region_max/grid_get_region_mean/grid_copy/grid_sort/grid_sort_rows/grid_shuffle/grid_shuffle_rows/grid_shuffle_columns/grid_foreach/grid_filternonea board, a tilemap, anything naturally rows-and-columns
stack<T>one end only - the topstack_push/stack_pop/stack_peek/stack_copynoneundo history, a "back" trail - last in, first out
queue<T>one end in, the other end outqueue_push/queue_pop/queue_peek/queue_copycapacity must be a power of twoturn order, a spawn queue - first in, first out
map<K,V>values looked up by key, not positionmap_get/map_set/map_has/map_copycapacity must be a power of two; the key must be hashable (below)"what is alice's score" - looking things up by name
set<T>membership only - no value, no positionset_has/set_add/set_remove/set_copycapacity must be a power of two; the element must be hashable (below)"have I already visited this tile" - fast yes/no membership

array<T>, list<T> and grid<T> are the three containers that keep an accessor at all - plain [i] for the first two, [# x, y] for the grid. Accessors covers how that syntax works across all three in one place; Arrays covers the array<T> vs list<T> choice specifically, and Grids the two-number case. stack<T>, queue<T>, map<K,V> and set<T> drop indexing entirely and are reached only through their own named functions - a deliberate trade, covered on that same Accessors page and below.

The rules every container shares

However different the seven look in the table above, they all obey the same three rules - a fact worth knowing once rather than re-reading on every single page.

  1. Every container is bounded. There is no heap on a GBA to grow into, so a container's size is a real number, decided when you declare it, never "as many as the player needs this session." There are three ways to say the size, and they all mean the same thing: a /// @capacity N comment, the number inside the type (list<int, 16>), or the number in a function that makes the container (list_create(16, 0)). The functions are array_create, list_create, stack_create, queue_create, set_create, map_create and grid_create; see Writing a type out for the whole picture. Leave the size off in every one of them and you get 8. grid<T> alone needs a shape, not just a total - /// @capacity WxH (say 8x6), grid<int, 8, 6> or grid_create(8, 6, 0) - see Grids for why.
  2. Filling one up never crashes. Pushing, adding or setting into a container that is already full never stops the game and never corrupts anything. For nearly every function it also changes nothing and reports back false. There is one function that works the other way, list_insert: on a full list it keeps the new item and the last item is lost. The next section lines every case up. Ignore the return value and the container simply stops accepting new items; check it only when the game needs to react to "no room left."
  3. Reading past what is there never crashes either. Popping or peeking an empty stack<T>/queue<T>, or map_get-ing a key that was never set, hands back a plain default value instead - 0, 0.0, false, an empty string, or nothing for a ref<obj>, depending on what the container holds. That is a real, ordinary value, not an error marker, so the only way to tell "the container really held this" apart from "the container was empty/the key was missing" is to ask first - stack_empty/queue_empty/map_has/ set_has exist for exactly that.

When a container is full

"What happens when it is full?" is the first question to ask of any container with a fixed size, so here is every answer in one place. The rule behind all of them is the same: whatever would end up past the end is lost, and nothing crashes. For almost every function, what is lost is the new item. For list_insert it is the last item.

You do this to a full...What happensIt gives back
list, list_add(items, x)x is not added; everything already in the list staysfalse
list, list_insert(items, i, x) with i before the endx goes in at i; the last item falls off the end and is losttrue
list, list_insert(items, list_length(items), x)nothing changes - x is the one that would be past the endfalse
stack, stack_push(s, x)x is not pushed; the stack is unchangedfalse
queue, queue_push(q, x)x is not added; the queue is unchangedfalse
set, set_add(s, x) with a new xx is not added; the set is unchangedfalse
set, set_add(s, x) where x is already in itnothing to do, x is a membertrue
map, map_set(m, k, v) with a new keythe pair is not stored; the map is unchangedfalse
map, map_set(m, k, v) with a key that is already in itthe old value is replaced - a map does not grow when a value is replacedtrue

An array and a grid cannot be full in this way, because they have no add: every slot is there from the start. The box is a fixed size on purpose. A GBA has no spare memory to make it bigger while the game runs, so when there is no room something has to be left out, and these are the places that say which. If the lost item would matter, compare list_length or its twin with the size you gave the container, and only add when there is room.

Putting in order, mixing up, and going through every item

Four jobs have a function for the three containers that keep their items in a row or a block - the array, the list and the grid - and for no other:

JobArrayListGrid
put numbers or text in orderarray_sortlist_sortgrid_sort (each column or row on its own), grid_sort_rows (whole rows, by one column)
mix up the order, in placearray_shufflelist_shufflegrid_shuffle (the cells), grid_shuffle_rows, grid_shuffle_columns
run one of your scripts on every itemarray_foreachlist_foreachgrid_foreach
make a list of the items a script says yes toarray_filterlist_filtergrid_filter
take the items a script says no to out of the container itself(an array has a fixed size)list_keep(a grid has a fixed size)

A few rules hold for all of them:

The stack, the queue, the set and the map have none of these four. A stack and a queue exist to give things back in the order they went in, so sorting or shuffling one would undo what it is for. A set and a map have no order to put things in.

The power-of-two rule

queue<T>, map<K,V> and set<T> each have one extra rule array<T>/list<T>/grid<T>/ stack<T> do not: their capacity must be a power of two - 2, 4, 8, 16, 32, and so on, never 10 or 30. This is not a GBML house rule; it comes straight from the hardware container each one compiles to in the Game Boy Advance module (a fixed-size double-ended queue for queue<T>, a fixed-size hash table for map<K,V> and for set<T>), each of which refuses to build against any other size. The untagged default, 8, already satisfies it, so this only ever comes up if you write your own /// @capacity N on one of these three and pick a number that is not a power of two - the compiler catches it and names the next legal size up, rather than letting the mistake reach devkitARM as a raw assertion three headers deep in a file you never wrote.

What isn't offered: walking every key or member

map<K,V> and set<T> can tell you whether one specific key or value is present, in roughly constant time, however many the container holds - that speed is the entire reason to reach for either over a searched list<T>. What neither offers is a way to walk every entry - there is no for (k, v in some_map)-shaped statement anywhere in GBML, on purpose: the language has no iterator concept at all today, and even if it did, an unordered container's own storage is keyed off each key's hash, not the order you inserted things in, so "the order you'd walk it in" is not even a well-defined idea to expose.

The documented way around it, when a project genuinely needs every entry: keep a parallel list<T> of everything you put in - a list<K> alongside a map<K,V>'s own keys, or a plain list<T> alongside a set<T>'s own members - updated every time you map_set/map_remove or set_add/set_remove, and walk that with an ordinary indexed for loop. It costs one more container and one more line at every write, in exchange for a real, orderly walk whenever you need one.

Struct elements: what a map and a set will take

Structs in data containers covers the general pros, cons and how-to of putting a struct in any container. This section is narrower: the one place map<K,V> and set<T> genuinely part ways from every other container, worth stating plainly because it is easy to guess wrong:

A hashable key or element is one of: int, real, bool, string, color, ref<obj>, or any of your project's own asset ids (sprite, sound, song, font, tileset, room, path, effect, background). If a project needs to track "have I already seen this struct," the honest answer today is a plain list<T> searched with list_contains - slower (it walks the list, set<T> does not have to), but it accepts any element type at all, struct included.

See also

Every function on every container has its own page, linked from the table at the top. array<T> and list<T> are also covered as part of the language's own type system - see Variables, types and scope for how a container's type and capacity are declared, and Expressions and statements for how [i]/[# x, y] indexing actually reads.

See also: ArraysListslist_addlist_insertlist_filllist_create_fillGridsgrid_fillgrid_fill_regiongrid_get_region_sumgrid_get_region_maxgrid_get_region_meanstack_pushqueue_pushmap_setset_addAccessorsStructs in data containersWriting a type out, instead of in a commentarray_createarray_lengtharray_copygrid_creategrid_widthgrid_heightgrid_copylist_sortlist_shufflelist_foreachlist_filterarray_sortarray_shufflearray_foreacharray_filtergrid_sortgrid_shufflegrid_shuffle_rowsgrid_shuffle_columnsgrid_foreachgrid_filterlist_createlist_copystack_createstack_copyqueue_createqueue_copyset_createset_copymap_createmap_copy